package kr.ac.kaist.pointdaproject;

import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class CardListCustomAdapter extends CursorAdapter {

	private static final String PACKAGE = "kr.ac.kaist.pointdaproject";

	public CardListCustomAdapter(Context context, Cursor c) {
		super(context, c);
	}
	
	
	@Override
	public void bindView(View convertView, Context arg1, Cursor c) {

		TextView cardName = (TextView)convertView.findViewById(R.id.card_name);
		ImageView cardImage = (ImageView)convertView.findViewById(R.id.card_image);

		Resources res = convertView.getResources(); 
		String icon = c.getString(c.getColumnIndex(PointCard.Cards.COLUMN_POINTCARDS_IMAGE));
		if(icon == null) {
			icon = "card1";
		}
		int temp = res.getIdentifier(icon, "drawable", PACKAGE);

		cardImage.setImageResource(temp);
		cardName.setText(c.getString(c.getColumnIndex(PointCard.Cards.COLUMN_POINTCARDS_NAME)));

	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {  
		LayoutInflater inflater = LayoutInflater.from(context);
		
		View v = inflater.inflate(R.layout.customadapter, parent, false);
		
		return v;
	}

}
